import java.lang.reflect.Array;
import java.util.Arrays;

public class MyMarrayList implements Ilistable{
    public int[] elem;
    public int usedSized;
public static final int DEFAULT_CAPACITY=5;
    public MyMarrayList() {
        this.elem = new int[DEFAULT_CAPACITY];
    }
    public MyMarrayList(int initCapacity) {
        this.elem = new int[initCapacity];
    }

    @Override
    public void add(int data) {
if (isFull()){
    grow();
}
elem[usedSized++]=data;
    }

    @Override
    public void add(int pose, int data) {
     checkPose(pose);
      if (isFull()){
      grow();
      }
      for (int i=usedSized-1;i>=pose;i--){
          elem[i+1]=elem[i];}
          elem[pose]=data;
          usedSized++;

    }
     private void checkPose(int pose){
        if(pose>usedSized||pose<0)
            throw new  PosOutOfBoundsException("添加元素的时候位置不合法"+pose);
     }
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i <usedSized ; i++) {
           if(elem[i]==toFind){
            return true;
        }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i=0;i<usedSized;i++){
            if (elem[i]==toFind){
                return i;}
        }
        return -1;
    }
    private void checkPoseByGetOrSet(int pose){
        if(pose>=usedSized||pose<0)
            throw new LisyEmptException("获取元素时pos位置不合法："+pose);
    }


    @Override
    public int get(int pose) {
        checkPoseByGetOrSet(pose);
if(isEmpty()){
    throw new LisyEmptException("获取元素时，顺序表为空");
}
        return elem[pose];
    }

    @Override
    public void set(int pose, int value) {
        checkPoseByGetOrSet(pose);
        elem[pose]=value;
    }

    @Override
    public void remove(int toRemove) {
   int index=indexOf(toRemove);
  if (index==-1){
      System.out.println("没有你需要的数据");
      return;
}
  for (int i=index;i<usedSized;i++){
      elem[i]=elem[i+1];
  }
  usedSized--;
    }

    @Override
    public int size() {
        return usedSized;
    }

    @Override
    public void clear() {
        //如果是Sring就可以这样改
      //  for (int i=0;i<usedSized;i++){
        //    elem[i]=null;
        //}
              usedSized=0;
    }

    @Override
    public void display() {
        for (int i=0;i<usedSized;i++){
            System.out.print(elem[i]+" ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return usedSized==elem.length;//==bu不是赋值，是判断相等
    }

    @Override
    public boolean isEmpty() {
        return usedSized==0;
    }
    private void grow(){
        elem= Arrays.copyOf(elem,2*elem.length);
    }
    private void grow(int grownumber){
        elem= Arrays.copyOf(elem,grownumber*elem.length);
    }


}
